package com.sanerzone.smscenter.modules.sms.web.front;

import com.sanerzone.jeebase.common.utils.JedisUtils;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.jeebase.modules.sys.utils.UserUtils;
import com.sanerzone.smscenter.modules.account.entity.BaseAccountAmountLogs;
import com.sanerzone.smscenter.modules.account.service.BaseAccountAmountLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by xiaoqianlong on 2018/5/4.
 */

@Controller
@RequestMapping(value="/front/consume")
public class FrontConsumeController {

    @Autowired
    private BaseAccountAmountLogsService baseAccountAmountLogsService;

    @ModelAttribute
    public BaseAccountAmountLogs get(@RequestParam(required = false) String id) {
        BaseAccountAmountLogs entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = baseAccountAmountLogsService.get(id);
        }
        if (entity == null) {
            entity = new BaseAccountAmountLogs();
        }
        return entity;
    }

    //@RequiresPermissions("account:baseAccountAmountLogs:view")
    @RequestMapping(value = "weekConsume")
    public String weekConsume(BaseAccountAmountLogs baseAccountAmountLogs, HttpServletRequest request, HttpServletResponse response, Model model, String time) {
        String amount=null;
        try {
            amount=JedisUtils.get( "amount_sms_"+ UserUtils.getUser().getId());
        }catch (Exception e){
            e.printStackTrace();
            amount="余额获取有误";
        }

        if(time==null||time==""){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
            Date day=new Date();
            time = sdf.format(day);
        }

        baseAccountAmountLogs.setUserid(UserUtils.getUser().getId());
        String chooseTime=time;
        int []a=new int[7];
        for(int i=0;i<7;i++){
            String str=getWeekByDate(time,i);
            BaseAccountAmountLogs b=baseAccountAmountLogsService.findDayConsume(UserUtils.getUser().getId(), str);
            if(b==null){
                a[i]=0;
            }else {
                a[i]=-Integer.parseInt(b.getAmount());
            }
            System.out.println(a[i]);
        }
        baseAccountAmountLogs.setDayAmount(a);
        model.addAttribute(baseAccountAmountLogs);
        model.addAttribute(chooseTime);
        model.addAttribute( "amount",amount );
        return  "modules/sms/front/home";
    }

    private static String getWeekByDate(String time,int a) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(time));
        }catch (Exception e){
            e.printStackTrace();
        }
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, a);
        return sdf.format(cal.getTime());
    }
}
